//
//  HYCustomAlertView.m
//  XPlayer
//
//  Created by 张水 on 2023/11/21.
//

#import "HYCustomAlertView.h"
#import <HYUIKit/HYAlertShowAnimate.h>

#define kHeaderHeight 40
#define kActionCellHeight 60

@interface HYCustomAlertView ()

@property (nonatomic, strong) NSArray *actionTitles;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, copy) HYBNumberBlock actionBlock;

@end

@implementation HYCustomAlertView

- (void)dealloc
{
    CLog(@"dealloc -- %@",[self class]);
}

- (instancetype)initWithHeaderTitle:(NSString *)title 
                       actionTitles:(NSArray *)actionTitles
                        actionBlock:(HYBNumberBlock)actionBlock
{
    self = [super init];
    if(self){
        self.title = title;
        self.actionTitles = actionTitles;
        self.actionBlock = actionBlock;
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    CGFloat contentHeight = kHeaderHeight + self.actionTitles.count*kActionCellHeight;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(self);
        make.height.mas_equalTo(contentHeight);
    }];
    UIView *ggView;
    [self.contentView addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.contentView);
        make.height.mas_equalTo(kHeaderHeight-kOneScale);
    }];
    self.label.text = self.title;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#D1D1D1" darkModeHex:@"#232225"];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.label.mas_bottom);
        make.height.mas_equalTo(kOneScale);
    }];
    
    ggView = lineView;
    for (int i = 0; i < self.actionTitles.count;i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:self.actionTitles[i] forState:UIControlStateNormal];
        button.tag = i;
        button.titleLabel.font = FONT_SIZE(20);
        [button addTarget:self action:@selector(alertAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(ggView.mas_bottom);
            make.height.mas_equalTo(kActionCellHeight-kOneScale);
        }];
        if(i != self.actionTitles.count - 1){
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#D1D1D1" darkModeHex:@"#232225"];
            [self.contentView addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.contentView);
                make.top.equalTo(button.mas_bottom);
                make.height.mas_equalTo(kOneScale);
            }];
        }
        ggView = button;
    }
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.systemBlueColor forState:UIControlStateNormal];
    cancelButton.titleLabel.font = FONT_BOLD_SIZE(20);
    [cancelButton setBackgroundColor:AppContentBgThemeColor];
    cancelButton.layer.cornerRadius = 10.0;
    cancelButton.layer.masksToBounds = YES;
    [cancelButton addTarget:self action:@selector(hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(ggView.mas_bottom).offset(10);
        make.height.mas_equalTo(kActionCellHeight);
    }];
}

- (void)alertAction:(UIButton *)button
{
    if(self.actionBlock){
        self.actionBlock(@(button.tag));
    }
    [self hiddenView];
}
- (void)show
{
    CGFloat contentHeight = kHeaderHeight + self.actionTitles.count * kActionCellHeight + kActionCellHeight + 10 + (HOME_BAR_HEIGHT?:10);
    self.frame = CGRectMake(0, 0, kScreenWidth, contentHeight);
    [HYAlertShowAnimate showInView:[UIApplication sharedApplication].keyWindow
                         alertView:self
                         popupMode:View_Popup_Mode_Down
                           bgAlpha:0.5
                  outsideDisappear:YES];
}

- (void)hiddenView
{
    [HYAlertShowAnimate disappear];
}

#pragma mark - loadLazy
- (UIView *)contentView
{
    if(!_contentView){
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = AppContentBgThemeColor;
        _contentView.layer.cornerRadius = 10.0;
        _contentView.layer.masksToBounds = YES;
    }
    return _contentView;
}

- (UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.textColor = UIColor.lightGrayColor;
        _label.font = FONT_SIZE(14);
        _label.textAlignment = NSTextAlignmentCenter;
    }
    return _label;
}
@end
